package com.glory.dmegcpda.view

import android.app.ProgressDialog
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.annotation.Nullable
import androidx.databinding.DataBindingUtil
import com.glory.dmegcpda.R
import com.glory.dmegcpda.customView.SelectorPicDialog
import com.glory.dmegcpda.databinding.ActivityPatrolBinding
import com.glory.dmegcpda.utils.BitmapUtils
import com.glory.dmegcpda.viewmodel.PatrilModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils
import com.zhihu.matisse.Matisse
import kotlinx.android.synthetic.main.activity_patrol.*

/**
 * FileName: QualityPatrolActivity
 * Author: XJH
 * Date: 2021/11/23 16:49
 * Description:
 * History:
 */
class QualityPatrolActivity : BaseActivity<PatrilModel>(), View.OnClickListener,
    SelectorPicDialog.CameraAndAlbumListener {
    private var mBinding : ActivityPatrolBinding? = null
    private var mDialog : SelectorPicDialog? = null
    private var mEqpTypePop: FreeUI_GeneralPop? = null
    private var myDialog // 保存进度框
            : ProgressDialog? = null
    override fun initData() {
        mDialog = SelectorPicDialog()
        mDialog!!.setTitle("请选择照片")
        mDialog!!.setOnCameraAndAlbumListener(this)
        mDialog!!.setSelectVideo(true) //相册可以选择照片
        mDialog!!.setRecordVideo(false) //可以录制视频

        viewModel.defUI.callObserve.observe(this){
            when(it.type){
                404 ->{
                    viewModel.defUI.toastEvent.value = ToastMessageBean(it.item as String,false)
                }
                200 ->{
                    tvType.text = ""
                    etText.setText("")
                    viewModel.iconList.clear()
                    viewModel.mPdfBeanList.clear()
                    viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功",true)
                }

                100 ->{
                    tvType.text = ""
                    etText.setText("")
                }

            }
            if (myDialog!!.isShowing){
                myDialog!!.dismiss()
            }
        }

    }

    /**
     * 初始化识别进度框
     */
    private fun initProgress() {
        myDialog = ProgressDialog(this, ProgressDialog.THEME_HOLO_LIGHT)
        myDialog!!.setIndeterminateDrawable(
            resources.getDrawable(
                R.drawable.progress_ocr
            )
        )
        myDialog!!.setMessage("正在保存PDF文件...")
        myDialog!!.setCanceledOnTouchOutside(false)
        myDialog!!.setCancelable(false)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_patrol)
        mBinding!!.patrolViewModel = viewModel
        ivBack.setOnClickListener(this)
        tvTemporary.setOnClickListener(this)
        tvFinal.setOnClickListener(this)
        tvShowPDF.setOnClickListener(this)
        flEqpTypeList.setOnClickListener(this)
        ivAddIcon.setOnClickListener(this)

        initProgress()
    }

    override fun layoutId(): Int {
        return R.layout.activity_patrol
    }

    override fun onClick(p: View?) {
        when(p?.id){
            R.id.ivBack ->{
                if (viewModel.mPdfBeanList.isNotEmpty()){
                    FreeApi_DialogUtils.getConfirmAndCancelDialog(this,
                        "确定",
                        "取消",
                        "当前已有临时保存数据，退出界面后将清空数据，确定退出？",
                        { finish() }
                    ) {}.show(supportFragmentManager, "")
                    return
                }
                finish()
            }
            R.id.tvTemporary ->{
                if (viewModel.iconList.isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先选择图片！",false)
                    return
                }

                viewModel.tempSave(tvType.text.toString(),etText.text.toString())
            }
            R.id.tvFinal ->{
                if (viewModel.iconList.isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先选择图片！",false)
                    return
                }
                myDialog!!.show()
                viewModel.save(this,tvType.text.toString(),etText.text.toString())
            }
            R.id.tvShowPDF ->{
                startActivity(Intent(this@QualityPatrolActivity, LookPDFActivity::class.java))
            }
            R.id.flEqpTypeList ->{
                if (mEqpTypePop != null) {
                    if (mEqpTypePop!!.isShowing) {
                        mEqpTypePop!!.dismiss()
                    }
                } else {
                    mEqpTypePop = FreeApi_PopUtils.getNormalPop(
                        applicationContext, flEqpTypeList.width, viewModel.stepList, ivEqpTypeArrow
                    ) { codeS: String?, i: Int -> tvType.text = codeS }
                }
                FreeApi_PopUtils.setArrowShow(ivEqpTypeArrow)
                mEqpTypePop!!.showAsDropDown(flEqpTypeList, 0, 0)
            }
            R.id.ivAddIcon ->{
                mDialog!!.show(supportFragmentManager, "ICON_DIALOG")
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_CANCELED)return
        when (requestCode) {
            SelectorPicDialog.REQUEST_CODE_LOCAL -> {
                val pathList: List<String> = Matisse.obtainPathResult(data)
                HandleIconOrVideo(pathList[0], SelectorPicDialog.REQUEST_CODE_LOCAL)
            }
            SelectorPicDialog.REQUEST_CAMEAR -> {
                val path: String = BitmapUtils.getPath(this, BitmapUtils.imageUri)
                HandleIconOrVideo(path, SelectorPicDialog.REQUEST_VIDEO)
            }
        }
    }

    private fun HandleIconOrVideo(path: String, requestVideo: Int) {
        if (path.contains(".mp4")) {  //先判断地址是视频还是图片
            viewModel.defUI.toastEvent.value = ToastMessageBean("不能选择视频，请重新选择！",false)
        } else {
            //照片
            viewModel.iconList.add(path)
        }
    }

    override fun OnSuccess(type: String?) {
        when (type) {
            SelectorPicDialog.TYPE_VIDEO -> BitmapUtils.takePicture(
                this,
                SelectorPicDialog.REQUEST_CAMEAR
            )
            SelectorPicDialog.TYPE_ALBUM -> if (SelectorPicDialog.mSelectVideo) {
                BitmapUtils.selectPicAndTakePic(this, SelectorPicDialog.REQUEST_CODE_LOCAL, 1)
            } else {
                BitmapUtils.selectPicFromLocal(this, SelectorPicDialog.REQUEST_CODE_LOCAL, 1)
            }
            SelectorPicDialog.TYPE_CAMERA -> BitmapUtils.takePicture(
                this,
                SelectorPicDialog.REQUEST_CAMEAR
            )
        }
    }

    override fun OnFailed(type: String?) {
        viewModel.defUI.toastEvent.value = ToastMessageBean("当前权限开启失败，请重试！",false)
    }

    override fun OnFailedNotAsking(type: String?) {
        viewModel.defUI.toastEvent.value = ToastMessageBean("当前权限开启失败，并且设置为不在询问，请去设置界面重新开启！",false)
    }
}